package com.tuozixuan;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.googlecode.aviator.AviatorEvaluator;

/**
 * Aviator的内置函数用法
 */
public class AviatorBuildInFunctionDemo {

	public static void main(String[] args) {
		String str = "小哥哥带你使用Aviator";
		Map<String, Object> env = new HashMap<>();
		env.put("str", str);
		Long length = (Long) AviatorEvaluator.execute("string.length(str)", env);
		System.out.println(length);
		
		// 返回当前日期对象java.util.Date
		Date currentDate = (Date)AviatorEvaluator.execute("sysdate()");
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentDate));
		
		// 返回一个介于0-1的随机数，double类型
//		Double rand = (Double)AviatorEvaluator.execute("rand()");
//		System.out.println("rand:" + rand);
		
		// 打印对象，如果指定out，向out打印，否则输出到控制台 print([out],obj)
		AviatorEvaluator.execute("print('Hello World')");
		AviatorEvaluator.execute("println('Hello World')");
		
		// 返回System.currentTimeMillis
		Long now = (Long)AviatorEvaluator.execute("now()");
		System.out.println("now:" + now);
		
		// 将值的类型转为long
		Long longValue = (Long)AviatorEvaluator.execute("long('12')");
		System.out.println("longValue:" + longValue);
		
		// 将值的类型转为double
		Double doubleValue = (Double)AviatorEvaluator.execute("double('12')");
		System.out.println("doubleValue:" + doubleValue);
		
		// 将值的类型转为string
		String strValue = (String)AviatorEvaluator.execute("str('12')");
		System.out.println("strValue:" + strValue);
		
		// 将Date对象转化化特定格式的字符串
		env.put("date", new Date());
		String dateStrValue = (String)AviatorEvaluator.execute("date_to_string(date,'yyyy-MM-dd HH:mm:ss')", env);
		System.out.println("dateStrValue:" + dateStrValue);
		
		// 将特定格式的字符串转化为Date对象
		Date dateValue = (Date)AviatorEvaluator.execute("string_to_date('2021-09-27','yyyy-MM-dd')", env);
		System.out.println("dateValue:" + dateValue);
		
		// 判断s1是否包含s2，返回Boolean
		Boolean isContain = (Boolean)AviatorEvaluator.execute("string.contains('Hello World','Hello')");
		System.out.println("isContain:" + isContain);

		// 求字符串长度,返回Long
		Long strLength = (Long)AviatorEvaluator.execute("string.length('Hello World')");
		System.out.println("strLength:" + strLength);
		
		// s1是否以s2开始，返回Boolean
		Boolean isStartsWith = (Boolean)AviatorEvaluator.execute("string.startsWith('Hello World','Hello')");
		System.out.println("isStartsWith:" + isStartsWith);
		
		// s1是否以s2结尾,返回Boolean
		Boolean isEndsWith = (Boolean)AviatorEvaluator.execute("string.endsWith('Hello World','Hello')");
		System.out.println("isEndsWith:" + isEndsWith);
		
		// 截取字符串s，从begin到end，end如果忽略的话，将从begin到结尾，与java.util.String.substring一样
		String substring = (String)AviatorEvaluator.execute("string.substring('Hello World', 0, 5)");
		System.out.println("substring:" + substring);
		String substring1 = (String)AviatorEvaluator.execute("string.substring('Hello World', 5)");
		System.out.println("substring1:" + substring1);
		
		// java中的s1.indexOf(s2)，求s2在s1中的起始索引位置，如果不存在为-1
		Long indexOf = (Long)AviatorEvaluator.execute("string.indexOf('Hello World','World')");
		System.out.println("indexOf:" + indexOf);
		
		// Java里的String.split方法一致
		String[] strings = (String[])AviatorEvaluator.execute("string.split('Hello World',' ')");
		System.out.println("strings:" + strings[0] + " " + strings[1]);
		
		// 将集合里的元素以seperator为间隔连接起来形成字符串
		env.put("nameList", Arrays.asList("n1","n2","n3"));
		String names = (String)AviatorEvaluator.execute("string.join(nameList,',')", env);
		System.out.println("names:" + names);
		
		// Java里的String.replaceFirst 方法
		String replacefirst = (String)AviatorEvaluator.execute("string.replace_first('Hello','H', 'h')");
		System.out.println("replacefirst:" + replacefirst);
		
		// Java里的String.replaceAll方法 
		String replaceAll = (String)AviatorEvaluator.execute("string.replace_all('Hello','l', 'L')");
		System.out.println("replaceAll:" + replaceAll);
		
		
	}

}
